package top.jandmla.excel.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import top.jandmla.excel.bean.User;
import top.jandmla.excel.dao.UserMapper;
import top.jandmla.excel.pojo.QueryPageDTO;
import top.jandmla.excel.pojo.UserPojo;
import top.jandmla.excel.service.UserService;
import top.jandmla.excel.util.result.BaseResult;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Resource
    private UserMapper userMapper;

    /**
     * 通过用户名查询用户
     * 用于登录是，shiro检测比较
     * @param userName 用户名
     * @return 查询结果
     */
    @Override
    @Transactional(propagation= Propagation.REQUIRED,readOnly=true,rollbackFor={Exception.class})
    public UserPojo getPojoUser(String userName) {
        if (userName != null && !StringUtils.isEmpty(userName)){
            return userMapper.getPojoUser(userName);
        }
        return null;
    }

    /**
     * 查询所有角色
     * @param user 查询条件
     * @return 查询结果
     */
    @Override
    @Transactional(propagation= Propagation.REQUIRED,readOnly=true,rollbackFor={Exception.class})
    public List<UserPojo> getAllUserPojo(User user) {
        return userMapper.getAllUserPojo(user);
    }

    /**
     * 新增用户
     * @param user 用户对象
     * @return 新增结果
     */
    @Override
    @Transactional(propagation= Propagation.REQUIRED,readOnly=false,rollbackFor={Exception.class})
    public BaseResult<String> insertUser(User user) {
        if (user == null || StringUtils.isEmpty(user.getUserName()) || StringUtils.isEmpty(user.getPassword()))
            return BaseResult.fail(1,"参数传输不合法！");
        user.setId(UUID.randomUUID().toString().replaceAll("-",""));
        user.setStatus("0");
        return userMapper.insert(user) > 0 ? BaseResult.success("插入成功！") : BaseResult.fail(1,"插入失败");
    }

    /**
     * 修改用户信息
     * @param user 用户数据
     * @return 修改结果
     */
    @Override
    @Transactional(propagation= Propagation.REQUIRED,readOnly=false,rollbackFor={Exception.class})
    public BaseResult<String> updateUser(User user) {

        //获取登录用户
        UserPojo userPojo = (UserPojo) SecurityUtils.getSubject().getPrincipal();

        //判断是否修改自己
        if (user != null && StringUtils.equals(userPojo.getId(), user.getId())){
             return updateUserByAdmin(user);
        }
        return BaseResult.fail(1,"你的权限不够哦！无法修改其他人的信息，请和管理员联系！");
    }


    /**
     * 超级管理员修改用户信息
     * @param user 用户信息
     * @return 修改结果
     */
    @Override
    @Transactional(propagation= Propagation.REQUIRED,readOnly=false,rollbackFor={Exception.class})
    public BaseResult<String> updateUserByAdmin(User user){
        if (StringUtils.isEmpty(user.getUserName()) || StringUtils.isEmpty(user.getPassword()) || StringUtils.isEmpty(user.getId())){
            return BaseResult.fail(1,"非法参数传递！");
        }
        QueryWrapper<User> qw = new QueryWrapper<>();
        qw.eq("id",user.getId());
        if (userMapper.selectCount(qw) < 1)
            return BaseResult.fail(1,"不存在该角色，请先添加！");

        user.setStatus("0");
        return userMapper.updateById(user) > 0 ? BaseResult.success("修改成功！") : BaseResult.fail(1,"发生未知错误！");
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRED,readOnly=true,rollbackFor={Exception.class})
    public IPage<User> selectAllUserAndPage(QueryPageDTO<User> dto) {
        QueryWrapper<User> qw = new QueryWrapper<>();
        if (dto != null && dto.getQuery() != null){
            User user = dto.getQuery();
            if (user.getId() != null)
                qw.eq("id",user.getId());
            if (StringUtils.isEmpty(user.getUserName()))
                qw.like("user_name",user.getUserName());
            if (StringUtils.isEmpty(user.getStatus()))
                qw.eq("status",user.getStatus());
        }
        return userMapper.selectPage(dto.getPage(),qw);
    }


}
